<?php
/**
 * @Notes:【】
 * @User: zwc
 * @Date: 2022-05-06 17:58
 */

namespace App\Listener\WebSocket;

use App\Event\Talk\ChatEvent;
use App\Event\Talk\ChatReadEvent;
use App\Event\Talk\StartChatEvent;
use App\Event\TalkEvent;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * @Listener()
 */
class TalkMessageListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            TalkEvent::class
        ];
    }

    protected $event = [
        'chat' => ChatEvent::class,
        'start_chat' => StartChatEvent::class,
        'read' => ChatReadEvent::class
    ];

    public function process(object $event)
    {
        switch ($event->event_name) {
            case 'chat':
                $params['token'] = $event->token;
                $params['data'] = $event->data;
                break;
            case 'start_chat':
                $params['token'] = $event->token;
                $params['friend_id'] = $event->data['friend_id'];
                break;
            case 'read':
                $params['friend_id'] = $event->data['friend_id'];
                $params['token'] = $event->token;
                break;
        }

        event()->dispatch(new $this->event[$event->event_name](...$params));
        // TODO: Implement process() method.
    }

}